<?php

use GatewayWorker\Lib\Db;
use GatewayWorker\Lib\Gateway;


class Events
{
    public static function onConnect($client_id)
    {

    }

    public static function onMessage($client_id, $message)
    {
//        Gateway::sendToClient($client_id, $message.",\"AI-THINKER_123\"");
//        return;

        try {
            $db1 = Db::instance('db1');
            $message = str_replace(array("\r\n", "\r", "\n"), "", $message);
            $insert_id = $db1->insert('test')->cols(array('name' => "client_id=" . $client_id . " --- " . $message))->query();

//            >AI-THINKER_71D4B6,FE00,0255*

            if (strncmp($message, ">", 1) == 0) {
                $v = explode("*", $message);
                if (count($v) > 1) {
                    $value = $v[0];//>AI-THINKER_71D4B6,FE00,0255
                    $v1 = explode(",", $v[0]);
                    if (count($v1) > 2) {
                        $deviceid = str_replace(">", '', $v1[0]);//AI-THINKER_71D4B6
                        if (strlen($deviceid) < 5) {
                            return; //唯一且不能为空
                        }
                        $command = $v1[1];//FE00
                        $content = $v1[2];//0255

                        $id = $db1->select('id')
                            ->from('fa_wu_hjdevice')
                            ->where('deviceid= :deviceid')
                            ->bindValues(array('deviceid' => $deviceid))
                            ->single();

                        if (empty($id)) {
                            $data['deviceid'] = $deviceid;
                            //不插入， 设备看test手动添加
//                            $device_id = $db1->insert('fa_wu_hjsb')->cols($data)->query();
                            return;
                        }

                        $data = array(
                            'client_id' => $client_id,
                            'isonline' => 1,
                            'update_time' => date('Y-m-d H:i:s', time())
                        );

                        $value1 = intval($content);
                        $value2 = 0;
                        $value3 = 0;
                        if ($command === "FE00") {
                            //FE00 溫度
                            $value1 = $value1 / 10;
                            $data['wd_newvalue'] = $value1;
                        } else if ($command === 'FE01') {
                            //FE01 濕度
                            $value1 = $value1 / 10;
                            $data['sd_newvalue'] = $value1;
                        } else if ($command === "FE02") {
                            //油槽 A
                            $data['yc_a_newvalue'] = $value1;
                        } else if ($command === 'FE03') {
                            //油槽 B
                            $data['yc_b_newvalue'] = $value1;
                        } else if ($command === "FE04") {
                            //三相馬達A
                            $value1 = $value1 / 10; //0055 電流1
                            $value2 = intval($v1[3]) / 10;//0224 電流2
                            $value3 = intval($v1[4]) / 10;//0224 電流3
                            $data['sxmda_a_newvalue1'] = $value1;
                            $data['sxmda_a_newvalue2'] = $value2;
                            $data['sxmda_a_newvalue3'] = $value3;
                        } else if ($command === 'FE05') {
                            //三相馬達B
                            $value1 = $value1 / 10; //0055 電流1
                            $value2 = intval($v1[3]) / 10;//0224 電流2
                            $value3 = intval($v1[4]) / 10;//0224 電流3
                            $data['sxmda_b_newvalue1'] = $value1;
                            $data['sxmda_b_newvalue2'] = $value2;
                            $data['sxmda_b_newvalue3'] = $value3;
                        }  else if ($command === 'FE06') {
                            //三相馬達C
                            $value1 = $value1 / 10; //0055 電流1
                            $value2 = intval($v1[3]) / 10;//0224 電流2
                            $value3 = intval($v1[4]) / 10;//0224 電流3
                            $data['sxmda_c_newvalue1'] = $value1;
                            $data['sxmda_c_newvalue2'] = $value2;
                            $data['sxmda_c_newvalue3'] = $value3;
                        }  else if ($command === 'FE07') {
                            //三相馬達D
                            $value1 = $value1 / 10; //0055 電流1
                            $value2 = intval($v1[3]) / 10;//0224 電流2
                            $value3 = intval($v1[4]) / 10;//0224 電流3
                            $data['sxmda_d_newvalue1'] = $value1;
                            $data['sxmda_d_newvalue2'] = $value2;
                            $data['sxmda_d_newvalue3'] = $value3;
                        } else if ($command === "FE08") {
                            $data['sxmda_code'] = $content;
                        } else if ($command === "FE09") {
                            $data['zzycgj_code'] = $content;
                        } else if ($command === "FE99") {
                            //>AI-THINKER_71D4B6,FE99,0000*
                        }

                        $db1->update('fa_wu_hjdevice')->cols($data)->where("id=" . $id)->query();

                        if ($command === "FE99") {
                            //心跳包不记录
                        } else {
                            //插入记录
                            $data = array(
                                'hjdevice_id' => $id,
                                'commandtype' => $command,
                                'value1' => $value1,
                                'value2' => $value2,
                                'value3' => $value3,
                                'commandinfo' => $message,
                            );
                            $db1->insert('fa_wu_hjdevicelog')->cols($data)->query();
                        }

                        Gateway::sendToClient($client_id, "OK");
                    }
                }
            }


        } catch (\Exception $e) {
            exit(250);
        } catch (\Error $e) {
            exit(250);
        }

    }

    public static function onClose($client_id)
    {
        $db1 = Db::instance('db1');
        $device_id = $db1->select('id')
            ->from('fa_wu_hjsb')
            ->where('client_id= :client_id')
            ->orderByDESC(array('update_time'))
            ->limit(1)
            ->bindValues(array('client_id' => $client_id))
            ->single();

        if (!empty($device_id)) {
            $data = array(
                'isonline' => 0,
                'update_time' => date('Y-m-d H:i:s', time())
            );
            $db1->update('fa_wu_hjsb')->cols($data)->where("id=" . $device_id)->query();
        }
    }


}
